/**
 * May 16, 20141:48:42 PM
 */
package elitech.vietnam.copyweatherfashion.fragments.self;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.adapters.StyleGridAdapter;
import elitech.vietnam.copyweatherfashion.config.Const;
import elitech.vietnam.copyweatherfashion.dialog.WarningDialogFragment;
import elitech.vietnam.copyweatherfashion.entities.Post;
import elitech.vietnam.copyweatherfashion.fragments.MyAbstractFragment;
import elitech.vietnam.copyweatherfashion.wsclient.Service;

/**
 * @author Cong
 */
public class SelfContentFragment extends MyAbstractFragment implements OnRefreshListener2<GridView> {

	private static final String	TAG		= "CONTENT_TAG";

	MainActivity				mActivity;

	String						mTag	= "";
	List<Post>					mData;

	PullToRefreshGridView		mGrid;
	StyleGridAdapter			mAdapter;

	public static Bundle newArs(String tag) {
		Bundle bundle = new Bundle();
		bundle.putString(TAG, tag);
		return bundle;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mTag = getArguments().getString(TAG);
		mData = (mTag.equals(SelfFragment.TAG_MINE)) ? mActivity.mSelfList1 : mActivity.mSelfList2;
		View view = inflater.inflate(R.layout.styler_tabcontent, container, false);

		mGrid = (PullToRefreshGridView) view.findViewById(R.id.mainLayout);
		mAdapter = new StyleGridAdapter(mActivity, R.layout.style_item, mData);
		mAdapter.setHeartMode(mTag.equals(SelfFragment.TAG_MINE) ? false : true);
		mGrid.setAdapter(mAdapter);

		mGrid.setMode(Mode.PULL_FROM_END);
		mGrid.setOnRefreshListener(this);

		return view;
	}

	@Override
	public void onStart() {
		super.onStart();
		new GetData().execute();
		if (mGrid.isRefreshing())
			mGrid.onRefreshComplete();
	}

	@Override
	public void onResume() {
		super.onResume();
		/*
		 * Refresh data if user have new post
		 */
		if (mActivity.mRequestCode == MainActivity.REQUEST_NEWPOST && mTag.equals(SelfFragment.TAG_MINE)) {
			new GetData().execute();
		}
		mActivity.mRequestCode = -1;
		mActivity.mResultIntent = null;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		mData = null;
	}

	class GetData extends AsyncTask<Integer, Integer, Boolean> {
		ArrayList<Post>	rs	= new ArrayList<>();

		@Override
		protected void onPreExecute() {
			showLoading();
		}

		@Override
		protected Boolean doInBackground(Integer... params) {
			int n = (mActivity.mUser == null) ? -1 : mActivity.mUser.Id;
			try {
				if (mTag.equals(SelfFragment.TAG_MINE))
					rs = new Service().getMemberStylePage(n, n, Const.COUNTRYCODE, 0, Const.LOADMORE_COUNT);
				else
					rs = new Service().getMemberLikedStylePage(n, Const.COUNTRYCODE, 0, Const.LOADMORE_COUNT);
			} catch (IOException | XmlPullParserException e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if (!isVisible())
				return;
			if (!result) {
				new WarningDialogFragment.Builder().setMessage(getString(R.string.cannot_connect_to_server)).build()
						.show(getFragmentManager());
			} else if (rs.isEmpty()) {
				showText(getString(R.string.nodata));
			} else {
				mData.clear();
				mData.addAll(rs);
				mAdapter.notifyDataSetChanged();
				showMain();
			}
		}
	}

	class LoadMoreData extends AsyncTask<Integer, Integer, ArrayList<Post>> {
		ArrayList<Post>	rs	= new ArrayList<>();

		@Override
		protected ArrayList<Post> doInBackground(Integer... params) {
			int n = (mActivity.mUser == null) ? -1 : mActivity.mUser.Id;
			try {
				if (mTag.equals(SelfFragment.TAG_MINE))
					rs = new Service().getMemberStylePage(n, n, Const.COUNTRYCODE, mData.size(), Const.LOADMORE_COUNT);
				else
					rs = new Service()
							.getMemberLikedStylePage(n, Const.COUNTRYCODE, mData.size(), Const.LOADMORE_COUNT);
			} catch (IOException | XmlPullParserException e) {
				e.printStackTrace();
			}
			return rs;
		}

		@Override
		protected void onPostExecute(ArrayList<Post> result) {
			if (!isVisible())
				return;
			if (result == null) {
			} else if (result.isEmpty()) {
			} else {
				mData.addAll(rs);
				mAdapter.notifyDataSetChanged();
			}
			mGrid.onRefreshComplete();
		}
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
		new LoadMoreData().execute();
	}
}
